package com.dragon.pagingsample

import android.os.Handler
import android.os.Looper
import java.util.concurrent.Executor
import java.util.concurrent.Executors

/*
* 一般在application中使用
*
* */
class AppExecutors private constructor(
    private val mDiskIO: Executor,
    private val mNetworkIO: Executor,
    private val mMainThread: Executor
) {
    constructor() : this(
        Executors.newSingleThreadExecutor(), Executors.newFixedThreadPool(3),
        MainThreadExecutor()
    ) {
    }

    fun diskIO(f : () -> Unit) {
        return mDiskIO.execute(f)
    }

    fun networkIO(f : () -> Unit) {
        return mNetworkIO.execute(f)
    }

    fun mainThread():Executor {
        return mMainThread
    }

    private class MainThreadExecutor : Executor {
        private val mainThreadHandler = Handler(Looper.getMainLooper())
        override fun execute(command: Runnable) {
            mainThreadHandler.post(command)
        }
    }
}